﻿namespace Pyramid.Tests
{
    using System.Collections.Generic;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Pyramid.IO;

    [TestClass]
    public class MemoryStreamTest
    {
        [TestMethod]
        public void ShouldWriteAndReadATryte()
        {
            var target = new MemoryStream();
            Tryte value = 123;

            target.WriteTryte(value);
            target.Position = 0;
            Int27 result = target.ReadTryte();

            Assert.AreEqual((Int27)value, result);
        }

        [TestMethod]
        public void ShouldWriteAndReadMultipleTrytes()
        {
            var target = new MemoryStream();
            var values = new List<Tryte> { (Tryte)123, (Tryte)456, (Tryte)789 };
            var results = new List<Tryte>();

            foreach (var value in values)
            {
                target.WriteTryte(value);
            }

            target.Position = 0;

            Int27 result = target.ReadTryte();

            while (result != Int27.MinValue)
            {
                results.Add((Tryte)result);

                result = target.ReadTryte();
            }

            CollectionAssert.AreEqual(values, results);
        }

        [TestMethod]
        public void ShouldReturnInt27MinValueOnReadByteWhenEmpty()
        {
            var target = new MemoryStream();

            Int27 result = target.ReadTryte();

            Assert.AreEqual(Int27.MinValue, result);
        }
    }
}
